/*
 * @# NoneCacheCase.java 2011-11-7 下午05:05:33
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.experiments.redis;

import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ianswer.common.orm.PublicDao;

/**
 * @author alvin hwang
 */
public class NoneCacheCase {

	private static Logger log = LoggerFactory.getLogger(NoneCacheCase.class);
	
	private PublicDao publicDao;
	
	public NoneCacheCase() {
		publicDao = DataUtils.getBean("publicDao", PublicDao.class);
	}
	
	public RedisDemo get(Long id) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		RedisDemo data = publicDao.querySingle("redisDemo.get", id);
		stopWatch.stop();
		//log.debug("NoneCacheCase get {} 运行时间为: {}", id, stopWatch.toString());
		
		return data;
	}
	
	public RedisDemo getByCache(Long id) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		RedisDemo data = publicDao.querySingle("redisDemo.getByCache", id);
		stopWatch.stop();
		//log.debug("NoneCacheCase getByCache {} 运行时间为: {}", id, stopWatch.toString());
		
		return data;
	}
	
	public void run(int loops, boolean same, boolean cache) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		
		for (int i = 0; i < loops; i++) {
			if (cache) {
				getByCache(Long.valueOf(same ? 1 : (i + 1)));
			} else {
				get(Long.valueOf(same ? 1 : (i + 1)));
			}
		}
		
		stopWatch.stop();
		if (cache) {
			//log.debug("NoneCacheCase getByCache 遍历次数: {} 总运行时间为: {}", loops, stopWatch.toString());
		} else {
			//log.debug("NoneCacheCase get 遍历次数: {} 总运行时间为: {}", loops, stopWatch.toString());
		}
	}
	
	public static void main(String[] args) {
		NoneCacheCase testcase = new NoneCacheCase();
		
		testcase.run(5000, true, false);
		
		log.debug("done");
	}
}
